var div = document.getElementById("chat");
var contain = document.getElementById("contain");
div.style.height = (window.screen.availHeight - 70) + "px";

RongIMClient.init("p5tvi9dsph774")//这是初始化，需要填参数就是你的APPKEY。这个不难理解。
var token = "B19XSjJN+P8TWIGKABcKuDj6+KBJaK0wnXPlxQOASrOyz+jwt5IW8Ecq3glkjKNOKdxdg2uGSIggLm9xk2rHXA==";


// 连接融云服务器。
RongIMClient.connect(token, {
        onSuccess: function(userId) {
        console.log("Login successfully."+ userId);
        //userId是申请token时的填写的id，到时候可以封装在下面的extra中传过去
    },
    onTokenIncorrect: function() {
        console.log('token无效');
    },
    onError:function(errorCode){
        var info = '';
        switch (errorCode) {
            case RongIMLib.ErrorCode.TIMEOUT:
                info = '超时';
                break;
            case RongIMLib.ErrorCode.UNKNOWN_ERROR:
                info = '未知错误';
                break;
            case RongIMLib.ErrorCode.UNACCEPTABLE_PaROTOCOL_VERSION:
                info = '不可接受的协议版本';
                break;
            case RongIMLib.ErrorCode.IDENTIFIER_REJECTED:
                info = 'appkey不正确';
                break;
            case RongIMLib.ErrorCode.SERVER_UNAVAILABLE:
                info = '服务器不可用';
                break;
        }
        console.log(errorCode);
    }
});


 // 连接状态监听器
RongIMClient.setConnectionStatusListener({
    onChanged: function (status) {
        switch (status) {
            //链接成功
            case RongIMLib.ConnectionStatus.CONNECTED:
                console.log('链接成功');
                break;
            //正在链接
            case RongIMLib.ConnectionStatus.CONNECTING:
                console.log('正在链接');
                break;
            //重新链接
            case RongIMLib.ConnectionStatus.DISCONNECTED:
                console.log('断开连接');
                break;
            //其他设备登陆
            case RongIMLib.ConnectionStatus.KICKED_OFFLINE_BY_OTHER_CLIENT:
                console.log('其他设备登陆');
                break;
            //网络不可用
            case RongIMLib.ConnectionStatus.NETWORK_UNAVAILABLE:
                console.log('网络不可用');
                break;
        }
    }
});


 // 消息监听器
RongIMClient.setOnReceiveMessageListener({
    // 接收到的消息
    onReceived: function (message) {
        // 判断消息类型
                console.log(message);
//              console.log(message.content.content);
        switch(message.messageType){


//              console.log(message.content.content);
//message接受到的消息（包含发送的信息，也可以在extra中添加要传递的值，如：时间等）

            case RongIMClient.MessageType.TextMessage:
            	console.log("TextMessage")
				str = '<div class="chat_list"><div class="head_photo lf"><img src="img/dog3.png"/></div><div class="chat_conten lf"><div class="Triangle_left"></div><p>'+ message.content.content +'</p></div><div class="clear"></div></div>';
				//生成聊天内容
				div.innerHTML += str;
				//在页面追加你要生成的内容
                break;
            case RongIMClient.MessageType.ImageMessage:
                console.log("ImageMessage")
				str = '<div class="chat_list"><div class="head_photo lf"><img src="img/dog3.png"/></div><div class="chat_conten lf"><div class="Triangle_left"></div><img src="'+ message.content.imageUri +'"/></div><div class="clear"></div></div>';
				//生成聊天内容
				div.innerHTML += str;
				//在页面追加你要生成的内容
                break;
            case RongIMClient.MessageType.DiscussionNotificationMessage:
                // do something...
                console.log("DiscussionNotificationMessage")
                break;
            case RongIMClient.MessageType.LocationMessage:
                // do something...
                console.log("LocationMessage")
                break;
            case RongIMClient.MessageType.RichContentMessage:
                // do something...
                console.log("RichContentMessage")
                break;
            case RongIMClient.MessageType.DiscussionNotificationMessage:
                // do something...
                console.log("DiscussionNotificationMessage")
                break;
            case RongIMClient.MessageType.InformationNotificationMessage:
                // do something...
                console.log("InformationNotificationMessage")
                break;
            case RongIMClient.MessageType.ContactNotificationMessage:
                // do something...
                console.log("ContactNotificationMessage")
                break;
            case RongIMClient.MessageType.ProfileNotificationMessage:
                // do something...
                console.log("ProfileNotificationMessage")
                break;
            case RongIMClient.MessageType.CommandNotificationMessage:
                // do something...
                console.log("CommandNotificationMessage")
                break;
            case RongIMClient.MessageType.CommandMessage:
                // do something...
                console.log("CommandMessage")
                break;
            case RongIMClient.MessageType.UnknownMessage:
                // do something...
                console.log("UnknownMessage")
                break;
            default:
            // 自定义消息
            // do something...
        }
		div.scrollTop = div.scrollHeight;
    }

});


function getMessage(){
	var value = contain.value.replace(/(^\s*)|(\s*$)/g,"");
	if(value.length == 0){
		return
	}

	str = '<div class="chat_list"><div class="head_photo rf"><img src="img/dog5.png"/></div><div class="chat_conten rf"><div class="Triangle_right"></div><p>'+ value +'</p></div><div class="clear"></div></div>';
    //生成聊天内容
    div.innerHTML += str;
    //在页面追加你要生成的内容
	div.scrollTop = div.scrollHeight;

    // 定义消息类型,文字消息使用 RongIMLib.TextMessage
    var msg = new RongIMLib.TextMessage({content:value,extra:"附加要传递的值"});

    var conversationtype = RongIMLib.ConversationType.PRIVATE; // 私聊
    var targetId = "111"; // 目标 Id


    RongIMClient.getInstance().sendMessage(conversationtype, targetId, msg, {
                // 发送消息成功

                onSuccess: function (message) {
                  //message 为发送的消息对象并且包含服务器返回的消息唯一Id和发送消息时间戳
                    console.log("Send successfully");

                },
                onError: function (errorCode, message) {
                    var info = '';
                    switch (errorCode) {
                        case RongIMLib.ErrorCode.TIMEOUT:
                            info = '超时';
                            break;
                        case RongIMLib.ErrorCode.UNKNOWN_ERROR:
                            info = '未知错误';
                            break;
                        case RongIMLib.ErrorCode.REJECTED_BY_BLACKLIST:
                            info = '在黑名单中，无法向对方发送消息';
                            break;
                        case RongIMLib.ErrorCode.NOT_IN_DISCUSSION:
                            info = '不在讨论组中';
                            break;
                        case RongIMLib.ErrorCode.NOT_IN_GROUP:
                            info = '不在群组中';
                            break;
                        case RongIMLib.ErrorCode.NOT_IN_CHATROOM:
                            info = '不在聊天室中';
                            break;
                        default :
                            info = "x";
                            break;
                    }
                    console.log('发送失败:' + info);
                }
            }
    );
}


document.onkeydown = function(e){
	console.log(event.keyCode)
	if(event.keyCode == 13){
		getMessage()
	}
}